package browserUI
{
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import settings.Settings;
	
	public class SettingsPanel extends Sprite
	{
		
		private var _mask:Shape;
		private var _content:Sprite;
		private var _homeInput:TextField
		
		public var open:Boolean;
		
		public function SettingsPanel()
		{
			super();
			
			_mask=new Shape();
			_mask.graphics.beginFill(0xFFFFFF);
			_mask.graphics.drawRect(-500,0,500,200);
			addChild(_mask);
			
			_content=new Sprite();
			var m:Matrix=new Matrix();
			m.createGradientBox(1,200,Math.PI/2,0,0);
			_content.graphics.beginGradientFill(GradientType.LINEAR,[0xA0A2A5,0xE8E8E8],[1,1],[0,255],m);
			_content.graphics.drawRoundRectComplex(0,0,500,200,0,0,25,0);
			_content.mask=_mask;
			_content.filters=[new DropShadowFilter(1,90,0,0.7,2,2)];
			addChild(_content);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			var titleText:TextField=new TextField();
			titleText.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,30);
			titleText.embedFonts=true;
			titleText.selectable=false;
			titleText.x=15;
			titleText.width=485;
			titleText.height=40;
			titleText.text="Settings";
			_content.addChild(titleText);
			
			var homeLabel:TextField=new TextField();
			homeLabel.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15);
			homeLabel.embedFonts=true;
			homeLabel.selectable=false;
			homeLabel.x=15;
			homeLabel.y=60;
			homeLabel.width=90;
			homeLabel.height=40;
			homeLabel.text="Home Page:";
			_content.addChild(homeLabel);
			
			_homeInput=new TextField();
			_homeInput.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15);
			_homeInput.embedFonts=true;
			_homeInput.type=TextFieldType.INPUT;
			_homeInput.x=105;
			_homeInput.y=60;
			_homeInput.width=200;
			_homeInput.height=25;
			_homeInput.background=true;
			_content.addChild(_homeInput);
			
			var useCurrentButton:Sprite=new Sprite();
			useCurrentButton.x=320;
			useCurrentButton.y=60;
			useCurrentButton.graphics.beginFill(0x27A0C1);
			useCurrentButton.graphics.drawRoundRect(0,0,90,25,10);
			_content.addChild(useCurrentButton);
			useCurrentButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				_homeInput.text=Navbar(parent).url;
			});
			
			var useCurrentLabel:TextField=new TextField();
			useCurrentLabel.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15,0,null,null,null,null,null,TextFormatAlign.CENTER);
			useCurrentLabel.embedFonts=true;
			useCurrentLabel.selectable=false;
			useCurrentLabel.x=0;
			useCurrentLabel.y=0;
			useCurrentLabel.width=90;
			useCurrentLabel.height=25;
			useCurrentLabel.text="Use Current";
			useCurrentButton.addChild(useCurrentLabel);
			
			var saveButton:Sprite=new Sprite();
			saveButton.x=400;
			saveButton.y=150;
			saveButton.graphics.beginFill(0x27A0C1);
			saveButton.graphics.drawRoundRect(0,0,90,25,10);
			_content.addChild(saveButton);
			saveButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				var url:String=_homeInput.text;
				if(url.search(/^(ht|f)tp(s?)\:\/\//)==-1)
					url="http://"+url;
				MTBrowser.currentSettings.homeURL=url;
				Settings.saveSettings(MTBrowser.currentSettings);
				_refreshInputs();
			});
			
			var saveLabel:TextField=new TextField();
			saveLabel.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15,0,null,null,null,null,null,TextFormatAlign.CENTER);
			saveLabel.embedFonts=true;
			saveLabel.selectable=false;
			saveLabel.x=0;
			saveLabel.y=0;
			saveLabel.width=90;
			saveLabel.height=25;
			saveLabel.text="SaveSettings";
			saveButton.addChild(saveLabel);
			
			var fullScreenButton:Sprite=new Sprite();
			fullScreenButton.x=300;
			fullScreenButton.y=150;
			fullScreenButton.graphics.beginFill(0x27A0C1);
			fullScreenButton.graphics.drawRoundRect(0,0,90,25,10);
			_content.addChild(fullScreenButton);
			fullScreenButton.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				if(stage.displayState==StageDisplayState.FULL_SCREEN_INTERACTIVE)
					stage.displayState=StageDisplayState.NORMAL;
				else
					stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
			});
			
			var fullScreenLabel:TextField=new TextField();
			fullScreenLabel.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15,0,null,null,null,null,null,TextFormatAlign.CENTER);
			fullScreenLabel.embedFonts=true;
			fullScreenLabel.selectable=false;
			fullScreenLabel.x=0;
			fullScreenLabel.y=0;
			fullScreenLabel.width=90;
			fullScreenLabel.height=25;
			fullScreenLabel.text="Full Screen";
			fullScreenButton.addChild(fullScreenLabel);
			
			_refreshInputs();
		}
		
		private function _refreshInputs():void{
			_homeInput.text=MTBrowser.currentSettings.homeURL;
		}
		
		private function onEnterFrame(event:Event):void{
			var target:Number=open?-500:0;
			if(target!=_content.x){
				_content.x-=(_content.x-target)*0.5;
				if(Math.abs(_content.x-target)<5)
					_content.x=target
			}
		}
	}
}